package com.jewaton.authmanage.pojo;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 用户表
 * @author magiczeng
 *
 */
public class UserInfo implements java.io.Serializable, UserDetails {
	
	private static final long serialVersionUID = 8039695510043968978L;
	
	//用户表主键
	private Long id;
	
	//用户名（登录账号）
	private String username;
	
	//真实姓名
	private String realname;
	
	//登录密码
	private String password;
	
	//邮箱地址
	private String email;
	
	//联系电话号码（固话）
	private String telphone;
	
	//手机号码
	private String mobile;
	
	//通讯地址
	private String address;
	
	//账户是否被锁 true没锁 false锁了 默认true
	private Boolean accountNonLocked = true;
	
	//账户认证（密码）是否过期 true没有 false过期了 默认true
	private Boolean credentialsNonExpired = true;
	
	//账户是否过期 true没有 false过期了 默认true
	private Boolean accountNonExpired = true;
	
	//用户状态 1为启用0为禁用 默认1
	private Boolean enabled = true;
	
	//是否内置用户 1是 0不是 默认0
	private Boolean systemdefine = false;
	
	//用户下的授权列表
	private Set<GrantedAuthority> authorities;
	
	//该用户下的角色列表
	private List<Role> roleList;
	
	//该用户下的资源列表
	private List<Resource> resourceList;

	
	public UserInfo(){
		
	}
	
	public UserInfo(String username, String password, String[] roles){
		this.username = username;
		this.password = password;
		if(roles!=null){
			authorities = new HashSet<GrantedAuthority>();
			for (String role : roles) {
				authorities.add(new SimpleGrantedAuthority(role));
			}
		}
	}
	
	//测试用
	public void setAuthoritiesOt(String[] roles){
		if(roles!=null){
			authorities = new HashSet<GrantedAuthority>();
			for (String role : roles) {
				authorities.add(new SimpleGrantedAuthority(role));
			}
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/*public Boolean getAccountNonLocked() {
		return accountNonLocked;
	}*/

	public void setAccountNonLocked(Boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	/*public Boolean getCredentialsNonExpired() {
		return credentialsNonExpired;
	}*/

	public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	/*public Boolean getAccountNonExpired() {
		return accountNonExpired;
	}*/

	public void setAccountNonExpired(Boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	/*public Boolean getEnabled() {
		return enabled;
	}*/

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Set<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Set<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	// 获取当前登录用户详细信息必须重写hashCode和equals方法
	public int hashCode() {
		return this.getUsername().hashCode();
	}

	public boolean equals(Object object) {
		boolean flag = false;
		if (object instanceof UserDetails) {
			UserDetails user = (UserDetails) object;
			if (user.getUsername().equals(this.getUsername()))
				flag = true;
		}
		return flag;
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.accountNonExpired==null?false:this.accountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.accountNonLocked==null?false:this.accountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.credentialsNonExpired==null?false:this.credentialsNonExpired;
	}

	@Override
	public boolean isEnabled() {
		//this.enabled = isAccountNonExpired() && isAccountNonLocked() 
		//		&& isCredentialsNonExpired();
		return this.enabled==null?false:this.enabled;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Resource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(List<Resource> resourceList) {
		this.resourceList = resourceList;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Boolean isSystemdefine() {
		return this.systemdefine==null?false:this.systemdefine;
	}

	public void setSystemdefine(Boolean systemdefine) {
		this.systemdefine = systemdefine;
	}

}
